package com.hxit.movieserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxit.movieserver.entity.Movie;
import com.hxit.movieserver.service.MovieFeign_User;
import com.hxit.movieserver.service.MovieService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hxit.common.vo.Result;

@RestController
@Api(tags = "电影接口")
@RequestMapping("/api/movie/")
public class MovieController {

    @Autowired
    private MovieService movieService;


    @ApiOperation(value = "新增电影")
    @PostMapping("save")
    public Result save(Movie movie){
        return movieService.save(movie)?Result.ok():Result.fail("新增失败");
    }

    @ApiOperation(value = "修改电影")
    @PostMapping("update")
    public Result update(Movie movie){
        return movieService.updateById(movie)?Result.ok():Result.fail("修改失败");
    }

    @ApiOperation(value="删除电影")
    @PostMapping("delete")
    public Result delete(Movie movie) {
        return movieService.removeById(movie) ? Result.ok() : Result.fail("删除失败!");
    }

    @ApiOperation(value="查询电影")
    @GetMapping("all")
    public Result all(){
        System.out.println(movieService.list());
        return Result.ok(movieService.list());
    }

    @ApiOperation(value="件查询和分页")
    @GetMapping("page")
    public Result page(String name , @ApiParam("第几页") Integer page ,@ApiParam("一页的数据") Integer limit){
        LambdaQueryWrapper<Movie> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(name)){
            wrapper.like(Movie::getName,name);
        }
        return Result.ok(movieService.page(new Page<>(page,limit),wrapper));
    }

    @Autowired
    private MovieFeign_User movieFeign_user;

    @ApiOperation(value = "查询用户评价")
    @GetMapping("getUserMovie")
    public Result findUserByMovieId(Integer movieid){
        return movieFeign_user.findUserMovies(movieid);
    }

}
